﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WcfServer.Classes.Utility;

namespace WcfServer.Classes.Client
{
    [Serializable]
    public class PersonalMessage
    {
        public string SenderUserID { set; get; }
        public string PMessage { set; get; }
        public DateTime TimeStamp { set; get; }

        public byte[] signedMessageChecksum { set; get; }

        public PersonalMessage(byte[] data, byte[] key)
        {
            byte[] temp = Utility.EncryptionHandler.DecryptAES(data, key);
            var pm = SerializerHandler<PersonalMessage>.DeSerializeInput(temp);
            //TODO: CHECK FOR CHECKSUM VALIDITY (SIGN) FOR NON REPUDIATION AND AUTHENTICITY! NOT HERE!
            this.PMessage = pm.PMessage;
            this.SenderUserID = pm.SenderUserID;
            this.TimeStamp= pm.TimeStamp;
            this.signedMessageChecksum = pm.signedMessageChecksum;
        }

        public PersonalMessage() { }

        public byte[] Send(byte[] key)
        {
            signedMessageChecksum = EncryptionHandler.EncryptRSA(EncryptionHandler.HashSHA256(SerializerHandler<string>.SerializeInput(PMessage)),SenderUserID);
            var pm = SerializerHandler<PersonalMessage>.SerializeInput(this);
            return EncryptionHandler.EncryptAES(pm, key);
        }
    }
}
